package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.YzcLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserSubscribe;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserSubscribeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	@YzcLogin(required = false)
	@Operation(summary = "判断用户是否购买声音列表")
	@PostMapping("userIsPaidTrack/{albumId}")
	public Result<Map<Long, Integer>> userIsPaidTrack(@PathVariable Long albumId, @RequestBody List<Long> trackIdList) {
		//	获取用户Id
		Long userId = AuthContextHolder.getUserId();
		//	调用服务层方法
		Map<Long, Integer> map = userInfoService.userIsPaidTrack(userId, albumId, trackIdList);
		//	返回map 集合数据
		return Result.ok(map);
	}

	@YzcLogin
	@Operation(summary = "判断用户是否购买过专辑")
	@GetMapping("isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
		// 获取到用户Id
		Long userId = AuthContextHolder.getUserId();
		// 调用服务层方法
		Boolean flag = userInfoService.isPaidAlbum(userId, albumId);
		return Result.ok(flag);
	}

	@YzcLogin
	@Operation(summary = "根据专辑id获取用户支付过的声音id列表")
	@GetMapping("findUserPaidTrackList/{albumId}")
	public Result findUserPaidTrackList(@PathVariable Long albumId){
		// 获取用户Id
		Long userId = AuthContextHolder.getUserId();
		// 根据用户Id 与 专辑Id 获取到已购买的声音Id 集合列表
		List<Long> trackIdList = this.userInfoService.findUserPaidTrackList(userId,albumId);
		// 返回声音Id 集合数据
		return Result.ok(trackIdList);
	}

	@Operation(summary = "更新Vip到期失效状态")
	@GetMapping("updateVipExpireStatus")
	public Result updateVipExpireStatus() {
		userInfoService.updateVipExpireStatus();
		return Result.ok();
	}

	@Operation(summary = "查找用户订阅页面")
	@GetMapping("findUserSubscribePage/{page}/{limit}")
	public Result findUserSubscribePage(@Parameter(name = "page",description = "当前页面",required = true)
											@PathVariable Long page,
										@Parameter(name = "limit",description = "每页记录数",required = true)
											@PathVariable Long limit,
										@Parameter(name = "trackInfoQuery",description = "查询对象",required = false)
										UserSubscribe userSubscribe){
		userSubscribe.setUserId(AuthContextHolder.getUserId());
		Page<UserSubscribeVo> subscribeVoPage = new Page<>(page, limit);
		IPage<UserSubscribeVo>  subscribeVoIPage= userInfoService.findUserSubscribePage(subscribeVoPage,userSubscribe);
		return Result.ok(subscribeVoIPage);
	}
}

